//
//  HeadLineViewController.m
//  Product_NEWS
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 FCH. All rights reserved.
//
//T1348647853363
#import "HeadLineViewController.h"
#import "LoopScrollView.h"
#import "ThreeImageCell.h"
#import "OneimageCell.h"
#import "BigImageCell.h"
#import "NewsListModel.h"
#import "ImagesDetailController.h"
#import "ReadDetailController.h"
#import "HeadLineDetailController.h"
@interface HeadLineViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,assign)BOOL isUpLoading;
@property(nonatomic,assign)int count;
@property(nonatomic,retain)NSMutableArray *LoopArray;
@property(nonatomic,retain)NSMutableArray *newsListArray;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation HeadLineViewController
- (void)dealloc{
    [_myTableView release];
    [_LoopArray release];
    [_hud release];
    [super dealloc];
}


-(void)viewWillAppear:(BOOL)animated{

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.898 green:0.063 blue:0.000 alpha:1.000];
    
    
    
    }


- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self initUI];
    [self addHeadView];
    [self addHeader];
    [self addFooter];
    

}
- (NSMutableArray *)LoopArray {
    if (!_LoopArray) {
        self.LoopArray = [NSMutableArray array];
    }
    return [[_LoopArray retain] autorelease];
}
- (NSMutableArray *)newsListArray {
    if (!_newsListArray) {
        self.newsListArray = [NSMutableArray array];
    }
    return [[_newsListArray retain] autorelease];
}

-(void)initUI
{

    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, 375 * SCREEN_WIDTH,  512 *SCREEN_HEIGHT)];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
 //注册cellINdentifier
    [self.myTableView registerClass:[ThreeImageCell class] forCellReuseIdentifier:@"images"];
    [self.myTableView registerClass:[BigImageCell class] forCellReuseIdentifier:@"bigImage"];
    [self.myTableView registerClass:[OneimageCell class] forCellReuseIdentifier:@"common"];
     [self.view addSubview:self.myTableView];
    
    [_myTableView release];



}

-(void)addHeader
{
    
    self.myTableView.headerHidden = NO;
    __block HeadLineViewController *vc = self;
    
    [self.myTableView addHeaderWithCallback:^{
        
        vc.count = 120;
        vc.isUpLoading = NO;
        [vc GetPersonList:vc.count];
        
    }];
    [self.myTableView headerBeginRefreshing];
}

-(void)addFooter
{
    __block HeadLineViewController *vc = self;
    [self.myTableView addFooterWithCallback:^{
        vc.count += 20;
        vc.isUpLoading  = YES;
        
        [vc GetPersonList:vc.count];
    }];
    
    
}

-(void)GetPersonList:(NSInteger)nextCursor
{
  
    
    
    
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    
    NSString *getstr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/headline/T1348647853363/%d-20.html", self.count];
    

    
    [mgr GET:getstr parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
       
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.newsListArray removeAllObjects];
        }
        NSMutableArray *allArr= [responseObject objectForKey:@"T1348647853363"];
        
        
        for (NSDictionary *dic in allArr) {
            
            NewsListModel *newListModel = [[NewsListModel alloc]init];
            [newListModel setValuesForKeysWithDictionary:dic];

            [self.newsListArray addObject:newListModel];
            if (newListModel.imgextra) {
                newListModel.flag = 1; //三张图片cell, //三张图片又是图片集
            }
            if (newListModel.imgType == 1) {
                newListModel.flag = 2; //一张大图cell
            }
         
            if (newListModel.skipID && [newListModel.skipType isEqualToString:@"photoset"])
                newListModel.isPhotoset = YES;
            
            
        }
        
        [self.newsListArray removeObjectAtIndex:0];

        // 刷新表格
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        MBProgressHUD *mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        mb.mode = MBProgressHUDModeText;
        mb.labelText = @"网络不给力";
        [mb hide:YES afterDelay:0.5];
    }];
}



#pragma Loop

- (void)addHeadView{
    LoopScrollView *loopView = [[LoopScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 *SCREEN_WIDTH,  167 *SCREEN_HEIGHT)];
    self.myTableView.tableHeaderView = loopView;
    [loopView release];
}







#pragma tableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.newsListArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsListModel *model = self.newsListArray[indexPath.row];
    if (model.flag == 1) {
        ThreeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"images" forIndexPath:indexPath];
       
        tableView.rowHeight = 125 * SCREEN_HEIGHT;
        cell.listModel = model;
      
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        
        return cell;
    } else if(model.flag == 2) {
        BigImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bigImage" forIndexPath:indexPath];
        tableView.rowHeight = 210 * SCREEN_HEIGHT;
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
      
        return cell;
    }
    else{
        OneimageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"common" forIndexPath:indexPath];
        tableView.rowHeight = 110 * SCREEN_HEIGHT;
        cell.listModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
      
        return cell;
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsListModel *model = self.newsListArray[indexPath.row];
    
    if (model.isPhotoset) {
         ImagesDetailController *ImageDeatil = [[ImagesDetailController alloc] init];
        if ([model.skipID containsString:@"|"]) {
           //切数组
            NSArray *IDs = [model.skipID componentsSeparatedByString:@"|"];
            if (IDs) {
                model.skipID = IDs[1];
                ImageDeatil.skipTypeID = IDs[0];
            }
            
            ImageDeatil.skipID = model.skipID;
            
            self.navigationController.navigationBarHidden = YES;
            [self.navigationController pushViewController:ImageDeatil animated:YES];
            [ImageDeatil release];

    }
    }else {
        
        HeadLineDetailController *detailsController = [[HeadLineDetailController alloc] init];
        
        detailsController.docid = model.docid;
        detailsController.newsTitle = model.title;
        detailsController.flag = model.boardid;
        detailsController.url_3w = model.url_3w;
        
        
        [self.navigationController pushViewController:detailsController animated:YES];
        
        [detailsController release];
    }

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
